package com.yto.hrreport.event.buttons {
	import com.adobe.serialization.json.JSON;
	import com.yto.hrreport.beans.StatisticBBean;
	import com.yto.hrreport.command.ReportCommand;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class SaveButtonEvent extends ButtonEvent {
		
		private static var saveEvent : SaveButtonEvent = new SaveButtonEvent();
		private var param : Object;
		private static var parentName : String;
		private var mouseEvent : MouseEvent;
		public function SaveButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : SaveButtonEvent {
			SaveButtonEvent.parentName = parentName;
			return saveEvent;
		}
		
		public function saveHandler(event : MouseEvent) : void {

			param = ReportCommand.getInstance(parentName).initParam("onBoSave");
			
			var dataProvider : ArrayCollection = param.bodyGrid.dataProvider as ArrayCollection;
			var jsonProvider : Array = new Array()
			
			if(dataProvider == null || dataProvider.length == 0) {
				Alert.show("表体不能为空！" , "提示"  , Alert.OK , hrreport.ui); 
				
				return ;
			}
				
				
			for each(var data : Object in dataProvider) {
				var statisticB : StatisticBBean = new StatisticBBean();
				
				// 子表共有
				statisticB.rowno = data.rowno;
				statisticB.statistictype = data.statistictype;
				
				// 子表1
				statisticB.dimission = data.dimission;
				statisticB.e_person = data.e_person;
				statisticB.induty = data.induty;
				statisticB.s_person = data.s_person;
				statisticB.transin = data.transin;
				statisticB.transout = data.transout;
				
				// 子表2
				statisticB.endcount = data.endcount;
				statisticB.endcount_w = data.endcount_w;
				statisticB.comcount = data.comcount;
				statisticB.mngcount = data.mngcount;
				statisticB.procount = data.procount;
				statisticB.procount_acc = data.procount_acc;
				statisticB.skillcount = data.skillcount;
				statisticB.skillcount_acc = data.skillcount_acc;
				statisticB.expcount = data.expcount;
				statisticB.expcount_acc = data.expcount_acc;
				statisticB.roundcount = data.roundcount;
				statisticB.roundcount_acc = data.roundcount_acc;
				statisticB.cscount = data.cscount;
				
				// 子表3
				statisticB.total = data.total;
				statisticB.gjzj_fzc_count = data.gjzj_fzc_count;
				statisticB.fzj_zj_zszj_count = data.fzj_zj_zszj_count;
				statisticB.jl_gjjl_zj_count = data.jl_gjjl_zj_count;
				statisticB.gjzg_gjzy_count = data.gjzg_gjzy_count;
				statisticB.zg_count = data.zg_count;
				
				// 子表4
				statisticB.unitname = data.unitname;
				statisticB.totalman = data.totalman;
				statisticB.totalwoman = data.totalwoman;
				statisticB.dirunitcount = data.dirunitcount;
				statisticB.joinunitcount = data.joinunitcount;
				statisticB.compcount = data.compcount;
				statisticB.officecount = data.officecount;
				statisticB.clerkcount = data.clerkcount;
				statisticB.attendcount = data.attendcount;
				statisticB.opercount = data.opercount;
				statisticB.pilotcount = data.pilotcount;
				statisticB.othercount = data.othercount;
					
				// 子表5
				statisticB.unittype = data.unittype;
				statisticB.yeartotal = data.yeartotal;
				statisticB.dimmission = data.dimmission;
				statisticB.bnczj = data.bnczj;
				statisticB.byrzl = data.byrzl;
				statisticB.lzl = data.lzl; 
				
				jsonProvider.push(statisticB);
			}
			
			var bodyJson : String = JSON.encode(jsonProvider);
			
			PopUpManager.addPopUp(hrreport.waitWindow, hrreport.ui, true);    
			PopUpManager.centerPopUp(hrreport.waitWindow);
			
			mouseEvent = event;
			
			var tableServer : RemoteObject = new RemoteObject;
			tableServer.destination = IProcessor.TableProcessor;
			tableServer.saveQueryData(parentName , param.statisticPeriod , bodyJson);
			tableServer.addEventListener(ResultEvent.RESULT , saveResultHandler);
		}
		
		public function saveResultHandler(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null) {
				if(lastResult == "false") {
					Alert.show("保存时出现异常..." , "提示" , Alert.OK , hrreport.ui);
				} else {
					
					if(param.headData == null)
						param.headData = new ArrayCollection();
					
					var check : Boolean = false;
					
					for each(var data : Object in param.headData) {
						if(data.period == param.statisticPeriod && data.billtype == parentName)
							check = true;
					}
					
					if(!check) {
						var curData : Object = new Object;
						curData.period = param.statisticPeriod;
						curData.billtype = parentName;
						curData.pk_statistic = lastResult;
						param.headData.addItem(curData);
						
						param.headGrid.dataProvider = param.headData;
						
						param.headGrid.selectedIndex = param.headData.length - 1;
					}
					
					updateButtons(mouseEvent , "save" , param.headCanvas , true);
				}
			}
			
			PopUpManager.removePopUp(hrreport.waitWindow);
			
		}
		
	}
}